package com.neutech.controller;

import com.mybatisflex.core.paginate.Page;
import com.neutech.entity.Grade;
import com.neutech.entity.HomeWorkCommit;
import com.neutech.entity.Student;
import com.neutech.service.GradeService;
import com.neutech.service.HomeWorkCommitService;
import com.neutech.service.StudentService;
import com.neutech.vo.HomeWorkVo;
import com.neutech.vo.ResultJson;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import jakarta.annotation.Resource;
import com.neutech.entity.HomeWork;
import com.neutech.service.HomeWorkService;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import java.util.List;

/**
 * 作业 控制层。
 *
 * @author Administrator
 * @since 2025-04-30
 */
@RestController
@Tag(name = "作业接口")
@RequestMapping("/homeWork")
public class HomeWorkController {

    @Resource
    HomeWorkService homeWorkService;
    @Resource
    GradeService gradeService;
    @Resource
    StudentService studentService;
    @Resource
    HomeWorkCommitService homeWorkCommitService;

    /**
     * 添加作业。
     *
     * @param homeWork 作业
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("/add")
    @Operation(summary = "保存作业", description="保存作业")
    public ResultJson<Boolean> save(HomeWork homeWork) {
        System.out.println(homeWork);
        return ResultJson.success(homeWorkService.add(homeWork), "添加作业成功");
    }

    /**
     * 根据主键删除作业。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @PostMapping("remove/{id}")
    @Operation(summary = "根据主键作业", description="根据主键作业")
    public ResultJson<Boolean> remove(@PathVariable Integer id) {
        return ResultJson.success(homeWorkService.removeById(id));
    }

    /**
     * 根据主键更新作业。
     *
     * @param homeWork 作业
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PostMapping("update")
    @Operation(summary = "根据主键更新作业", description="根据主键更新作业")
    public ResultJson<Boolean> update(HomeWork homeWork) {
        return ResultJson.success(homeWorkService.updateById(homeWork));
    }

    /**
     * 查询所有作业。
     *
     * @return 所有数据
     */
    @GetMapping("/list")
    @Operation(summary = "查询所有作业", description="查询所有作业")
    public ResultJson<Page<HomeWork>> list(Integer pageNo, Integer pageSize, String name, Integer active) {
        return ResultJson.success(homeWorkService.list(pageNo, pageSize, name, active));
    }

    /**
     * 根据作业主键获取详细信息。
     *
     * @param id 作业主键
     * @return 作业详情
     */
    @GetMapping("getInfo")
    @Operation(summary = "根据主键获取作业", description="根据主键获取作业")
    public ResultJson<HomeWorkVo> getInfo(Integer id) {
        HomeWork homework = homeWorkService.getById(id);
        Grade grade = gradeService.getById(homework.getGradeId());
        List<Student> students = studentService.list(homework.getGradeId());
        List<HomeWorkCommit> homeWorkCommits = homeWorkCommitService.list(id);
        return ResultJson.success(new HomeWorkVo(students, grade, homework, homeWorkCommits));
    }

    /**
     * 分页查询作业。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    @Operation(summary = "分页查询作业", description="分页查询作业")
    public ResultJson<Page<HomeWork>> page(@Parameter(description="分页信息")Page<HomeWork> page) {
        return ResultJson.success(homeWorkService.page(page));
    }

}
